package com.qdc.datacenters.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qdc.datacenters.domain.entity.ChatContactMsgSensitive;
import com.qdc.datacenters.domain.entity.CorpCustomer;
import com.qdc.datacenters.domain.entity.CorpPermitUser;
import com.qdc.datacenters.domain.query.sensitive.SensitiveAddQuery;
import com.qdc.datacenters.domain.query.sensitive.SensitiveQuery;
import com.qdc.datacenters.domain.vo.sensitive.SensitiveVo;
import com.qdc.datacenters.enums.DeleteStatusEnum;
import com.qdc.datacenters.mapper.ChatContactMsgSensitiveMapper;
import com.qdc.datacenters.service.IChatContactMsgSensitiveService;
import com.qdc.datacenters.service.ICorpCustomerService;
import com.qdc.datacenters.service.ICorpPermitUserService;
import com.qdc.datacenters.service.ISysUserService;
import com.qdc.datacenters.utils.StrUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 聊天敏感词(ChatContactMsgSensitive)表服务实现类
 *
 * @author danmo
 * @since 2024-03-07 16:53:26
 */
@Slf4j
@Service
public class ChatContactMsgSensitiveServiceImpl extends BaseServiceImpl<ChatContactMsgSensitiveMapper, ChatContactMsgSensitive> implements IChatContactMsgSensitiveService {

    @Autowired
    private ICorpPermitUserService iCorpPermitUserService;

    @Autowired
    private ICorpCustomerService iCorpCustomerService;

    @Autowired
    private ISysUserService iSysUserService;

    @Override
    public void add(SensitiveAddQuery query) {
        ChatContactMsgSensitive sensitive = new ChatContactMsgSensitive();
        BeanUtils.copyProperties(query, sensitive);
        if (CollectionUtil.isNotEmpty(query.getRangeList())) {
            sensitive.setRange(String.join(",", query.getRangeList()));
        }
        sensitive.setRemindUser(JSONObject.toJSONString(query));
        save(sensitive);
    }

    @Override
    public void update(SensitiveAddQuery query) {
        ChatContactMsgSensitive sensitive = new ChatContactMsgSensitive();
        BeanUtils.copyProperties(query, sensitive);
        if (CollectionUtil.isNotEmpty(query.getRangeList())) {
            sensitive.setRange(String.join(",", query.getRangeList()));
        }
        sensitive.setRemindUser(JSONObject.toJSONString(query));
        updateById(sensitive);
    }

    @Override
    public void editStatus(SensitiveQuery query) {
        if(Objects.isNull(query.getId())){
            return;
        }
        ChatContactMsgSensitive sensitive = new ChatContactMsgSensitive();
        sensitive.setId(query.getId());
        sensitive.setStatus(query.getStatus());
        updateById(sensitive);
    }

    @Override
    public void delete(SensitiveQuery query) {

        Optional.ofNullable(query.getIds()).orElseGet(ArrayList::new).add(query.getId());
        if (CollectionUtil.isEmpty(query.getIds())) {
            return;
        }
        List<ChatContactMsgSensitive> list = query.getIds().stream().map(id -> {
            ChatContactMsgSensitive sensitive = new ChatContactMsgSensitive();
            sensitive.setId(id);
            sensitive.setDelFlag(DeleteStatusEnum.DELETE_YES.getIndex());
            return sensitive;
        }).collect(Collectors.toList());
        updateBatchById(list);
    }

    @Override
    public SensitiveVo get(SensitiveQuery query) {
        ChatContactMsgSensitive sensitive = getById(query.getId());
        if (Objects.isNull(sensitive)) {
            return null;
        }
        SensitiveVo sensitiveVo = new SensitiveVo();
        sensitiveVo.setId(sensitive.getId());
        sensitiveVo.setName(sensitive.getName());
        sensitiveVo.setWords(sensitive.getWords());
        sensitiveVo.setStatus(sensitive.getStatus());
        sensitiveVo.setRangeType(sensitive.getRangeType());
        if (ObjectUtil.notEqual(0, sensitive.getRangeType()) && StrUtils.isNotEmpty(sensitive.getRange())) {
            List<String> effectIds = Arrays.stream(sensitive.getRange().split(",")).collect(Collectors.toList());
            if (ObjectUtil.equal(1, sensitive.getRangeType())) {
                List<CorpPermitUser> permitUsers = iCorpPermitUserService.list(new LambdaQueryWrapper<CorpPermitUser>()
                        .in(CorpPermitUser::getUserId, effectIds));
                if (CollectionUtil.isNotEmpty(permitUsers)) {
                    Map<Long, String> permitUserMap = permitUsers.stream().collect(Collectors.toMap(CorpPermitUser::getUserId, CorpPermitUser::getCorpUserName, (t1, t2) -> t1));
                    List<SensitiveVo.EffectRange> effectRangeList = effectIds.stream().map(effectId -> {
                        SensitiveVo.EffectRange effectRange = new SensitiveVo.EffectRange();
                        effectRange.setEffectId(effectId);
                        effectRange.setEffectName(permitUserMap.get(Long.parseLong(effectId)));
                        return effectRange;
                    }).collect(Collectors.toList());
                    sensitiveVo.setRangeList(effectRangeList);
                }
            } else if (ObjectUtil.equal(2, sensitive.getRangeType())) {
                List<CorpCustomer> customerList = iCorpCustomerService.list(new LambdaQueryWrapper<CorpCustomer>()
                        .in(CorpCustomer::getId, effectIds));
                if (CollectionUtil.isNotEmpty(customerList)) {
                    Map<Long, String> customerMap = customerList.stream().collect(Collectors.toMap(CorpCustomer::getId, CorpCustomer::getName, (t1, t2) -> t1));
                    List<SensitiveVo.EffectRange> effectRangeList = effectIds.stream().map(effectId -> {
                        SensitiveVo.EffectRange effectRange = new SensitiveVo.EffectRange();
                        effectRange.setEffectId(effectId);
                        effectRange.setEffectName(customerMap.get(Long.parseLong(effectId)));
                        return effectRange;
                    }).collect(Collectors.toList());
                    sensitiveVo.setRangeList(effectRangeList);
                }
            }
        }
        return sensitiveVo;
    }

    @Override
    public List<SensitiveVo> getList(SensitiveQuery query) {
        List<SensitiveVo> list = new LinkedList<>();
        List<ChatContactMsgSensitive> sensitives = this.baseMapper.getList(query);
        Map<Long, String> permitUserMap = new HashMap<>();
        Map<Long, String> customerMap = new HashMap<>();
        if(CollectionUtil.isNotEmpty(sensitives)){
            String userIds = sensitives.stream().filter(sensitive -> ObjectUtil.equal(1, sensitive.getRangeType())).map(ChatContactMsgSensitive::getRange).collect(Collectors.joining(","));
            String customerIds = sensitives.stream().filter(sensitive -> ObjectUtil.equal(2, sensitive.getRangeType())).map(ChatContactMsgSensitive::getRange).collect(Collectors.joining(","));
            if (StrUtils.isNotEmpty(userIds)) {
                List<String> effectIds = Arrays.stream(userIds.split(",")).collect(Collectors.toList());
                List<CorpPermitUser> permitUsers = iCorpPermitUserService.list(new LambdaQueryWrapper<CorpPermitUser>()
                        .in(CorpPermitUser::getUserId, effectIds));
                if (CollectionUtil.isNotEmpty(permitUsers)) {
                    permitUserMap = permitUsers.stream().collect(Collectors.toMap(CorpPermitUser::getUserId, CorpPermitUser::getCorpUserName, (t1, t2) -> t1));
                }
            } else if (StrUtils.isNotEmpty(customerIds)) {
                List<String> effectIds = Arrays.stream(customerIds.split(",")).collect(Collectors.toList());
                List<CorpCustomer> customerList = iCorpCustomerService.list(new LambdaQueryWrapper<CorpCustomer>()
                        .in(CorpCustomer::getId, effectIds));
                if (CollectionUtil.isNotEmpty(customerList)) {
                    customerMap = customerList.stream().collect(Collectors.toMap(CorpCustomer::getId, CorpCustomer::getName, (t1, t2) -> t1));
                }
            }
            for (ChatContactMsgSensitive sensitive : sensitives) {
                SensitiveVo sensitiveVo = new SensitiveVo();
                BeanUtils.copyProperties(sensitive,sensitiveVo);
                List<String> effectIds = Arrays.stream(sensitive.getRange().split(",")).toList();
                if(ObjectUtil.equal(1, sensitive.getRangeType())){
                    Map<Long, String> finalPermitUserMap = permitUserMap;
                    List<SensitiveVo.EffectRange> effectRangeList = effectIds.stream().map(effectId -> {
                        SensitiveVo.EffectRange effectRange = new SensitiveVo.EffectRange();
                        effectRange.setEffectId(effectId);
                        effectRange.setEffectName(finalPermitUserMap.get(Long.parseLong(effectId)));
                        return effectRange;
                    }).collect(Collectors.toList());
                    sensitiveVo.setRangeList(effectRangeList);
                }else if(ObjectUtil.equal(2, sensitive.getRangeType())){
                    Map<Long, String> finalCustomerMap = customerMap;
                    List<SensitiveVo.EffectRange> effectRangeList = effectIds.stream().map(effectId -> {
                        SensitiveVo.EffectRange effectRange = new SensitiveVo.EffectRange();
                        effectRange.setEffectId(effectId);
                        effectRange.setEffectName(finalCustomerMap.get(Long.parseLong(effectId)));
                        return effectRange;
                    }).collect(Collectors.toList());
                    sensitiveVo.setRangeList(effectRangeList);
                }
                list.add(sensitiveVo);
            }
        }
        return list;
    }

    @Override
    public List<ChatContactMsgSensitive> getListByEffectId(SensitiveQuery query) {
        return this.baseMapper.getListByEffectId(query);
    }
}

